<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/article/ListAllArticle.class.php
 *
 * 列出所有的文章列表
 *
 * PHP Version 5
 * @package:   class.article
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: ListAllArticle.class.php,v 1.1 2006-09-24 14:38:33 ghw Exp $
 * @date:      $Date: 2006-09-24 14:38:33 $
 */


include_once CLASS_PATH.'main/BaseAction.class.php';

//包含我们需要的工具软件包
include_once CLASS_PATH.'article/CategoryUtil.class.php';
include_once CLASS_PATH.'article/ArticleUtil.class.php';



//include the language file
if ( file_exists(LANG_PATH.SYSTEM_LANG.'/ListCatagory.lang.php') ) {
   include_once LANG_PATH.SYSTEM_LANG.'/ListCatagory.lang.php';
}


class ListAllArticle extends BaseAction {

   /**
    * 数据库的连接
    */
   private $db;


   /**
    * 每一页显示的文章列表数
    */
   private $pre_page = 100;

   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 根据给出的文章分类分页显示文章的列表
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {


      //取得模板对象
      $smarty = $this->getSmarty();
      //显示头
      $categroy = CategoryUtil::getTopCategory($this->db);
      $smarty->assign('catagroy', $categroy);
      //取得分类的信息
      $catagory_info = CategoryUtil::getInformation($this->db, $id);
      $smarty->assign('catagory_id', $catagory_info['id']);
      $smarty->assign('catagory_name', $catagory_info['name']);


      //取得所有的文章分类信息
      $all_catagory = CategoryUtil::getAllCatagoryInfo($this->db);
      $smarty->assign('all_catagory', $all_catagory);



      $smarty->assign('clone_title', LCG_CLONE_TITLE.' 5anet.com');
      $smarty->assign('header_meta_content', LCG_CLONE_TITLE.'5anet.com');


      //取得本类下文章的列表
      //注意在这里我们需要取得一些变量来进行分页
      //取得页面
      $page = $this->getParameterFromGET('page');

      if ( !$page || $page <= 0 ) {
         $page = 1;
      }

      //求总的页面
      $total_page = ArticleUtil::getAllPage($this->db, $this->pre_page);

      if ( $page > $total_page && $total_page > 0 ) {
         $page = $total_page;
      }

      //页面
      $smarty->assign('now_page', $page);
      $smarty->assign('total_page', $total_page);

      $begin_page = 1;
      $end_page = $total_page;

      if ( $page <= 10 && $total_page >=10  ) {
         $end_page = 10; 
      } else if ( $page > 10  ) {
         if ( $page % 10 == 0 ) {
            //向前翻
            $end_page = $page; 
            $begin_page = $end_page - 9;
         } else if ( $page % 10 == 1 ) {
            //向后翻
            //确定开始的页数
            $begin_page = $page; 
            if ( $begin_page > $total_page ) {
               $begin_page = $page - 9;
            }
            if ( ( $begin_page + 9 ) > $total_page ) {
               $end_page = $total_page;
            } else {
               $end_page = $begin_page + 9;
            } 
   
         } else {
            $num = $page % 10;
            $pre_num = floor($page / 10 );
            $begin_page = $pre_num * 10 + 1;
            $end_page = $begin_page + 9;
         }
      }

      if ( $end_page > $total_page ) {
         $end_page = $total_page;
      }

      $nav_page_array = array();
      for( $i = $begin_page; $i<=$end_page; $i++ ) {
         array_push($nav_page_array, $i);
      }

      //帖子导航栏
      $smarty->assign('nav_page', $nav_page_array);

      $offset_page = ( $page - 1 ) * $this->pre_page;

      $article_list = ArticleUtil::getAllArticleList($this->db, $this->pre_page, $offset_page);

      $smarty->assign('article', $article_list);
      $smarty->display('listallarticle.tmpl');
   }

}

?>
